package com.sinba.common.system.dao;

import com.sinba.common.dao.BaseDAOImplHibernate;
import com.sinba.common.dto.PageDTO;
import com.sinba.common.system.dto.ActivationCodeDTO;
import com.sinba.common.system.entity.ActivationCode;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import java.util.Date;
import java.util.List;

public class ActivationCodeDAO
    extends BaseDAOImplHibernate<ActivationCode>
    implements IActivationCodeDAO
{
    public List<ActivationCode> findActivationCode( ActivationCodeDTO dto )
    {
        final DetachedCriteria dc = DetachedCriteria.forClass( ActivationCode.class );
        dc.add( Restrictions.eq( "activationCode",
                                 dto.getActivationCode(  ) ) );
        dc.add( Restrictions.eq( "type",
                                 dto.getType(  ) ) );
//		dc.add(Restrictions.ne("status","YES"));
        dc.add( Restrictions.eq( "category",
                                 dto.getCategory(  ) ) );
        dc.add( Restrictions.and( Restrictions.ge( "validDate",
                                                   new Date(  ) ),
                                  Restrictions.le( "createTime",
                                                   new Date(  ) ) ) );

        return super.getHibernateTemplate(  ).findByCriteria( dc );
    }

    public PageDTO emailAuthPages( ActivationCodeDTO activationCodeDTO, String sord, String sidx )
    {
        final DetachedCriteria dc = DetachedCriteria.forClass( ActivationCode.class );

        if ( ( sord != null ) && ! sord.equals( "" ) && ( sidx != null ) && ! sidx.equals( "" ) )
        {
            if ( sord.equals( "desc" ) )
            {
                dc.addOrder( Order.desc( sidx ) );
            } else{
                dc.addOrder( Order.asc( sidx ) );
            }
        }
        return super.findPageByCriteria( dc,
                                         activationCodeDTO.getStart(  ),
                                         activationCodeDTO.getLimit(  ) );
    }

	public ActivationCode findCodeByAuthStr(String authStr) {
        final DetachedCriteria dc = DetachedCriteria.forClass( ActivationCode.class );
        dc.add( Restrictions.eq( "authStr", authStr) );
        dc.addOrder( Order.desc("createTime") );
        List<ActivationCode> codes = super.getHibernateTemplate(  ).findByCriteria( dc );
        if ( codes != null && codes.size() > 0) {
            //System.out.println("ActivationCodeDAO.findCodeByAuthStr()" + codes.size());
        	return codes.get(0);
		}
		return null;
		//return findUniqueBy("authStr", authStr);
	}
	
	public ActivationCode getCodeByAuthStrAndType(String authStr,String authCode,String category){
		final DetachedCriteria dc = DetachedCriteria.forClass( ActivationCode.class );
        dc.add( Restrictions.eq( "authStr", authStr));
        dc.add(Restrictions.eq( "category", category));
        dc.add(Restrictions.eq( "status", "未使用"));
        dc.add(Restrictions.eq("activationCode", authCode));
        dc.addOrder( Order.desc("createTime") );
        List<ActivationCode> codes = super.getHibernateTemplate(  ).findByCriteria( dc );
        if ( codes != null && codes.size() > 0) {
            //System.out.println("ActivationCodeDAO.findCodeByAuthStr()" + codes.size());
        	return codes.get(0);
		}
		return null;
	}
	
	
}
